package com.changgou.collect.service.impl;

import com.changgou.collect.dao.CollectMapper;
import com.changgou.collect.dao.SkuMapper;
import com.changgou.collect.dao.SpuMapper;
import com.changgou.collect.pojo.Collect;
import com.changgou.collect.service.CollectService;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import entity.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:shenkunlin
 * @Description:Collect业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class CollectServiceImpl implements CollectService {

    @Autowired
    private CollectMapper collectMapper;
    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private SpuMapper spuMapper;
    @Autowired
    private IdWorker idWorker;

    /**
     * 添加商品收藏
     */
    @Override
    public void add(String skuid, String username) {


        Sku sku = skuMapper.selectByPrimaryKey(skuid);

        Collect collect = new Collect();
        collect.setCollectId(idWorker.nextId());
        collect.setUsername(username);
        collect.setName(sku.getName());
        collect.setPrice(sku.getPrice());
        collect.setImage(sku.getImage());
        collect.setSpuid(sku.getSpuId());

        Spu spu = spuMapper.selectByPrimaryKey(sku.getSpuId());
        collect.setSaleService(spu.getSaleService());

        collectMapper.insertSelective(collect);
    }

    /**
     * 删除收藏
     *
     * @param username
     * @param skuId
     */
    @Override
    public boolean deleteCollectByUsernameAndId(String username, String skuId) {
        Sku sku = skuMapper.selectByPrimaryKey(skuId);
        Long spuId = sku.getSpuId();

        Collect collect = new Collect();
        collect.setSpuid(spuId);
        collect.setUsername(username);

        int i = collectMapper.delete(collect);
        if (i == 1) {
            return true;
        }
        return false;

    }

    /**
     * 查询收藏列表
     *
     * @param username
     * @return
     */
    @Override
    public List<Collect> findList(String username) {
        Collect collect = new Collect();
        collect.setUsername(username);
        List<Collect> collects = collectMapper.select(collect);
        return collects;
    }


    /**
     * 是否已收藏
     *
     * @param skuid
     * @param username
     * @return
     */
    @Override
    public boolean isCollect(String skuid, String username) {
        Sku sku = skuMapper.selectByPrimaryKey(skuid);
        Collect collect = new Collect();
        collect.setSpuid(sku.getSpuId());
        collect.setUsername(username);

        List<Collect> collects = collectMapper.select(collect);

        if (collects != null && collects.size() > 0) {
            return true;
        }
        return false;
    }
}
